package com.mysite.seckill.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

    public static final String DATE_FORMAT = "yyyy-MM-dd";

    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static SimpleDateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT);

    private static SimpleDateFormat dateTimeFormatter = new SimpleDateFormat(DATE_TIME_FORMAT);

    public static Date parse(String source) {
        try {
            if (source.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
                return dateFormatter.parse(source);
            }
            if (source.matches("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$")) {
                return dateTimeFormatter.parse(source);
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        throw new RuntimeException("不支持的数据格式！");
    }
}
